//
//  LocationTool.m
//  HappyCarry
//
//  Created by fuguangxin on 15/5/7.
//  Copyright (c) 2015年 fuguangxin. All rights reserved.
//

#import "LocationTool.h"

@interface LocationTool()<BMKLocationServiceDelegate, BMKGeneralDelegate, BMKGeoCodeSearchDelegate>
@property (nonatomic, strong) BMKMapManager *mapManager;
@property (nonatomic, strong) BMKLocationService *locService;
@property (nonatomic, strong) BMKGeoCodeSearch *searcher;
@property (nonatomic, assign) BOOL start;
@end

@implementation LocationTool


+ (LocationTool *)share{
    static LocationTool *locationTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        locationTool = [[LocationTool alloc] init];
    });
    return locationTool;
}
- (BMKLocationService *)locService{
    if (!_locService) {
        //初始化BMKLocationService
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
    }
    return _locService;
}

- (BMKGeoCodeSearch *)searcher{
    if (!_searcher) {
        _searcher =[[BMKGeoCodeSearch alloc]init];
        _searcher.delegate = self;
    }
    return _searcher;
}


- (void)getLocation{

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
        _location = nil;
        return;
    }
    //设置定位精确度，默认：kCLLocationAccuracyBest
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyBest];
    //指定最小距离更新(米)，默认：kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:1];
    
    //启动LocationService
    [self.locService startUserLocationService];
}

- (void)getAddress{
    if (!_location) {
        return;
    }
    
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){_location.location.coordinate.latitude, _location.location.coordinate.longitude};
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
    BMKReverseGeoCodeOption alloc]init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [self.searcher reverseGeoCode:reverseGeoCodeSearchOption];
    if(flag)
    {
      NSLog(@"反geo检索发送成功");
    }
    else
    {
      NSLog(@"反geo检索发送失败");
    }
}

- (CLLocationDistance)getDistanceWithBMKMapPoint:(BMKMapPoint )point{
    BMKMapPoint userPoint = BMKMapPointForCoordinate(_location.location.coordinate);
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point,userPoint);
    return distance;
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _location = userLocation;
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
    [_locService stopUserLocationService];
    [self getAddress];
}

- (void)didFailToLocateUserWithError:(NSError *)error{
    
}
//接收反向地理编码结果
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        _address = result.address;
        
    }
    else {
        NSLog(@"抱歉，未找到结果");
        _address = @"未知位置";
    }
    if ([self.delegate respondsToSelector:@selector(getLocationAddress:)]) {
        [self.delegate getLocationAddress:_address];
    }
}

@end
